function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, _toPropertyKey(descriptor.key), descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
function _get() { if (typeof Reflect !== "undefined" && Reflect.get) { _get = Reflect.get.bind(); } else { _get = function _get(target, property, receiver) { var base = _superPropBase(target, property); if (!base) return; var desc = Object.getOwnPropertyDescriptor(base, property); if (desc.get) { return desc.get.call(arguments.length < 3 ? target : receiver); } return desc.value; }; } return _get.apply(this, arguments); }
function _superPropBase(object, property) { while (!Object.prototype.hasOwnProperty.call(object, property)) { object = _getPrototypeOf(object); if (object === null) break; } return object; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); Object.defineProperty(subClass, "prototype", { writable: false }); if (superClass) _setPrototypeOf(subClass, superClass); }
function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf ? Object.setPrototypeOf.bind() : function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); }
function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; }
function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } else if (call !== void 0) { throw new TypeError("Derived constructors may only return object or undefined"); } return _assertThisInitialized(self); }
function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; }
function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } }
function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf.bind() : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); }
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
import { Edge } from '@antv/x6';
import ELNode from "../node";
import { ConditionTypeEnum, LITEFLOW_EDGE } from "../../constant";
import NodeOperator from "./node-operator";

/**
 * 串行编排操作符：THEN。
 *
 * 例如一个串行编排(THEN)示例：
 * (1) EL表达式语法：THEN(a, b, c, d)
 * (2) JSON表示形式：
 * {
    type: ConditionTypeEnum.THEN,
    children: [
      { type: NodeTypeEnum.COMMON, id: 'a' },
      { type: NodeTypeEnum.COMMON, id: 'b' },
      { type: NodeTypeEnum.COMMON, id: 'c' },
      { type: NodeTypeEnum.COMMON, id: 'd' },
    ],
  }
  * (3) 通过ELNode节点模型进行表示的组合关系为：
                                          ┌─────────────────┐
                                      ┌──▶│  NodeOperator   │
                                      │   └─────────────────┘
                                      │   ┌─────────────────┐
                                      ├──▶│  NodeOperator   │
  ┌─────────┐    ┌─────────────────┐  │   └─────────────────┘
  │  Chain  │───▶│  ThenOperator   │──┤   ┌─────────────────┐
  └─────────┘    └─────────────────┘  ├──▶│  NodeOperator   │
                                      │   └─────────────────┘
                                      │   ┌─────────────────┐
                                      └──▶│  NodeOperator   │
                                          └─────────────────┘
 */
var ThenOperator = /*#__PURE__*/function (_ELNode) {
  _inherits(ThenOperator, _ELNode);
  var _super = _createSuper(ThenOperator);
  function ThenOperator(parent, children, properties) {
    var _this;
    _classCallCheck(this, ThenOperator);
    _this = _super.call(this);
    _defineProperty(_assertThisInitialized(_this), "type", ConditionTypeEnum.THEN);
    _defineProperty(_assertThisInitialized(_this), "parent", void 0);
    _defineProperty(_assertThisInitialized(_this), "children", []);
    _defineProperty(_assertThisInitialized(_this), "properties", void 0);
    _this.parent = parent;
    if (children) {
      _this.children = children;
    }
    _this.properties = properties;
    return _this;
  }

  /**
   * 创建新的节点
   * @param parent 新节点的父节点
   * @param type 新节点的子节点类型
   */
  _createClass(ThenOperator, [{
    key: "removeChild",
    value:
    /**
     * 删除指定的子节点
     * @param child 子节点
     */
    function removeChild(child) {
      if (this.children && this.children.length > 1) {
        return _get(_getPrototypeOf(ThenOperator.prototype), "removeChild", this).call(this, child);
      } else {
        return this.remove();
      }
    }

    /**
     * 转换为X6的图数据格式
     */
  }, {
    key: "toCells",
    value: function toCells() {
      var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
      this.resetCells();
      var children = this.children,
        cells = this.cells;
      var last;
      children.forEach(function (child) {
        child.toCells(options);
        var next = child.getStartNode();
        if (last) {
          cells.push(Edge.create({
            shape: LITEFLOW_EDGE,
            source: last.id,
            target: next.id
          }));
        }
        last = child.getEndNode();
      });
      return this.getCells();
    }

    /**
     * 获取当前节点的开始节点
     */
  }, {
    key: "getStartNode",
    value: function getStartNode() {
      return this.children[0].getStartNode();
    }

    /**
     * 获取当前节点的结束节点
     */
  }, {
    key: "getEndNode",
    value: function getEndNode() {
      return this.children[this.children.length - 1].getEndNode();
    }

    /**
     * 转换为EL表达式字符串
     */
  }, {
    key: "toEL",
    value: function toEL(prefix) {
      var type = this.type;
      if (prefix) {
        return "".concat(prefix).concat(type, "(\n").concat(this.children.map(function (x) {
          return x.toEL("".concat(prefix, "  "));
        }).join(', \n'), "\n").concat(prefix, ")").concat(this.propertiesToEL());
      }
      return "".concat(type, "(").concat(this.children.map(function (x) {
        return x.toEL();
      }).join(','), ")").concat(this.propertiesToEL());
    }
  }], [{
    key: "create",
    value: function create(parent, type) {
      var newNode = new ThenOperator(parent);
      newNode.appendChild(NodeOperator.create(newNode, type));
      return newNode;
    }
  }]);
  return ThenOperator;
}(ELNode);
export { ThenOperator as default };